package com.js.rtu.fragment.n;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.js.rtu.R;
import com.js.rtu.adapter.PagerFragmentAdapter;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.fragment.second.Cmd_22_07Fragment;
import com.js.rtu.fragment.second.Cmd_22_09Fragment;
import com.js.rtu.fragment.second.Cmd_24Fragment;
import com.js.rtu.fragment.second.Cmd_25Fragment;
import com.js.rtu.fragment.second.Cmd_27_0AFragment;
import com.js.rtu.fragment.second.Cmd_27_0DFragment;
import com.js.rtu.fragment.second.Cmd_29Fragment;
import com.js.rtu.fragment.second.Cmd_51Fragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 模拟量
 */
public class Pager3ViewPagerFragment extends BaseFragment {

    @BindView(R.id.view_pager)
    ViewPager2 viewPager;
    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    private String[] mTitle;


    public static Pager3ViewPagerFragment newInstance() {
        Pager3ViewPagerFragment fragment = new Pager3ViewPagerFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_pager3_viewpager;
    }

    //   private String [] mTitle={getResources().getString(R.string.tab_view_pager_kpi_statistics),
    //            getResources().getString(R.string.tab_view_pager_market_statistics),
    //            getResources().getString(R.string.tab_view_pager_project_statistics)
    //            };
    @Override
    protected void initView() {

        mTitle = _mActivity.getResources().getStringArray(
                R.array.tab_view_pager3);
        List<Fragment> list = new ArrayList<>();
        list.add(MLNFragment.newInstance());
        list.add(LLJFragment.newInstance());
        list.add(DIDOFragment.newInstance());
//        list.add(Cmd_29Fragment.newInstance());
//        loadRootFragment(R.id.Cmd_22_07Fragment, Cmd_22_07Fragment.newInstance());//22_07设置终端工作参数
//        loadRootFragment(R.id.Cmd_27_0AFragment, Cmd_27_0AFragment.newInstance());//设置参数限值,模拟量
//        loadRootFragment(R.id.Cmd_22_09Fragment, Cmd_22_09Fragment.newInstance());//压力表供电时间(ms)
//        loadRootFragment(R.id.Cmd_29Fragment, Cmd_29Fragment.newInstance());//报警总开关
//        loadRootFragment(R.id.Cmd_25Fragment, Cmd_25Fragment.newInstance());//流量计设置
//        loadRootFragment(R.id.Cmd_24Fragment, Cmd_24Fragment.newInstance());//DI采集控制
//        loadRootFragment(R.id.Cmd_27_0DFragment, Cmd_27_0DFragment.newInstance());//DI采集控制
//        loadRootFragment(R.id.Cmd_51Fragment, Cmd_51Fragment.newInstance());//DI采集控制

        PagerFragmentAdapter viewPagerAdapter = new PagerFragmentAdapter(_mActivity, list);

        viewPager.setAdapter(viewPagerAdapter);
        TabLayoutMediator mediator = new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(mTitle[position]);
                tab.setCustomView(makeTabView(position));
            }
        });
        mediator.attach();
        viewPager.setOffscreenPageLimit(1);//设置屏幕外加载页面数量
        initEvent();
        viewPager.setUserInputEnabled(false);//true:滑动，false：禁止滑动
    }

    private void initEvent() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabSelect(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabNormal(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void changeTabSelect(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        //            ImageView img_title = (ImageView) view.findViewById(R.id.img_title);
        TextView txt_title = (TextView) view.findViewById(R.id.textview);
        txt_title.setTextColor(ContextCompat.getColor(_mActivity, R.color.tab_viewpager_selecet));
        txt_title.setTextSize(16);
        //            if (txt_title.getText().toString().equals("One")) {
        //                img_title.setImageResource(R.drawable.tab_home_passed);
        //                viewPager.setCurrentItem(0);
        //            } else if (txt_title.getText().toString().equals("Two")) {
        //                img_title.setImageResource(R.drawable.tab_mine_passed);
        //                viewPager.setCurrentItem(1);
        //            } else {
        //                img_title.setImageResource(R.drawable.tab_info_passed);
        //                viewPager.setCurrentItem(2);
        //            }
    }

    private void changeTabNormal(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        //            ImageView img_title = (ImageView) view.findViewById(R.id.textview);
        TextView txt_title = (TextView) view.findViewById(R.id.textview);
        txt_title.setTextColor(ContextCompat.getColor(_mActivity, R.color.tab_viewpager_unselecet));
        txt_title.setTextSize(12);
        //            if (txt_title.getText().toString().equals("One")) {
        //                img_title.setImageResource(R.drawable.tab_home_normal);
        //            } else if (txt_title.getText().toString().equals("Two")) {
        //                img_title.setImageResource(R.drawable.tab_mine_normal);
        //            } else {
        //                img_title.setImageResource(R.drawable.tab_info_normal);
        //            }
    }


    private int[] pics = new int[]{R.mipmap.ic_blue_remote, R.mipmap.ic_blue_remote, R.mipmap.ic_blue_remote};

    /**
     * 引入布局设置图标和标题
     *
     * @param position
     * @return
     */
    public View makeTabView(int position) {
        View tabView = LayoutInflater.from(_mActivity).inflate(R.layout.tab_text_icon, null);
        TextView textView = tabView.findViewById(R.id.textview);
        ImageView imageView = tabView.findViewById(R.id.imageview);
        textView.setText(mTitle[position]);
//        imageView.setImageResource(pics[position]);
        if (position == 0) {
            textView.setTextColor(ContextCompat.getColor(_mActivity, R.color.tab_viewpager_selecet));
            textView.setTextSize(16);
        } else {
            textView.setTextColor(ContextCompat.getColor(_mActivity, R.color.tab_viewpager_unselecet));
            textView.setTextSize(12);
        }
        return tabView;
    }

}
